import Vue from 'vue'
import VueRouter from 'vue-router'
import { getToken } from '@/utils/auth'

Vue.use(VueRouter)

const routes = [
  {
    path: '',
    name: 'index',
    component: () => import('@/views/index/Index'),
    redirect: '/',
    children: [
      {
        path: '/',
        name: 'home',
        unauth: true,
        component: () => import('@/views/home/Index')
      },
      {
        path: '/course',
        name: 'course',
        component: () => import('@/views/course/Index')
      },
      {
        path: '/information',
        name: 'information',
        unauth: true,
        component: () => import('@/views/information/Index')
      },
      {
        path: '/information/:id',
        name: 'informationDetail',
        unauth: true,
        component: () => import('@/views/information/Detail')
      },
      {
        path: '/course/:id',
        name: 'couseDetail',
        component: () => import('@/views/course/Detail')
      },
      {
        path: '/notice',
        name: 'notice',
        component: () => import('@/views/notice/Index')
      },
      {
        path: '/login',
        name: 'login',
        unauth: true,
        component: () => import('@/views/login/Index')
      },
      {
        path: '/retrieve',
        name: 'retrieve',
        unauth: true,
        component: () => import('@/views/login/Retrieve')
      },
      {
        path: '/register',
        name: 'register',
        unauth: true,
        component: () => import('@/views/register/Index')
      }
    ]
  },
  {
    path: '/info',
    name: 'info',
    component: () => import('@/views/info/Index'),
    redirect: '/info/index',
    children: [
      {
        path: '/info/index',
        title: '我的主页',
        name: 'index',
        type: '我的主页',
        icon: '&#xe729;',
        component: () => import('@/views/info/index/Index')
      },
      {
        path: '/info/study',
        title: '我的课程',
        name: 'study',
        type: '我的主页',
        icon: '&#xe607;',
        component: () => import('@/views/info/study/Index')
      },
      {
        path: '/info/record',
        title: '学习记录',
        name: 'record',
        type: '我的主页',
        icon: '&#xeb7b;',
        component: () => import('@/views/info/record/Index')
      },
      {
        path: '/info/paper',
        title: '我的考试',
        name: 'paper',
        type: '我的主页',
        icon: '&#xeb7b;',
        component: () => import('@/views/info/paper/Index')
      },
      {
        path: '/info/answer',
        title: '答题记录',
        name: 'answer',
        type: '我的主页',
        icon: '&#xeb7b;',
        component: () => import('@/views/info/answer/Index')
      },
      {
        path: '/info/collect',
        title: '我的收藏',
        name: 'collect',
        type: '我的主页',
        icon: '&#xe600;',
        component: () => import('@/views/info/collect/Index')
      },
      {
        path: '/info/certificate',
        title: '我的证书',
        name: 'certificate',
        type: '我的主页',
        icon: '&#xe608;',
        component: () => import('@/views/info/certificate/Index')
      },
      {
        path: '/info/exam/:id',
        title: '考试',
        name: 'exam',
        type: '我的主页',
        hide: true,
        component: () => import('@/views/info/exam/Index')
      },
      {
        path: '/info/class',
        title: '我的班级',
        name: 'class',
        type: '教师园地',
        icon: '&#xe65f;',
        component: () => import('@/views/info/index/Index')
      },
      {
        path: '/info/student',
        title: '我的学生',
        name: 'student',
        type: '教师园地',
        icon: '&#xe7c0;',
        component: () => import('@/views/info/index/Index')
      },
      {
        path: '/info/center',
        title: '个人信息',
        name: 'center',
        type: '个人中心',
        icon: '&#xe618;',
        component: () => import('@/views/info/center/Index')
      },
      {
        path: '/info/helper',
        title: '小助手',
        name: 'helper',
        type: '个人中心',
        icon: '&#xe618;',
        component: () => import('@/views/info/index/Index')
      },
      {
        path: '/info/opinion',
        title: '意见反馈',
        name: 'opinion',
        type: '个人中心',
        icon: '&#xe661;',
        component: () => import('@/views/info/opinion/Index')
      },
      {
        path: '/info/password',
        title: '修改密码',
        name: 'password',
        type: '个人中心',
        icon: '&#xe606;',
        component: () => import('@/views/info/password/Index')
      },
      {
        path: '/info/account',
        title: '账号绑定',
        name: 'account',
        type: '个人中心',
        icon: '&#xe606;',
        component: () => import('@/views/info/account/Index')
      },
      {
        path: '/info/message',
        title: '我的消息',
        name: 'message',
        type: '个人中心',
        icon: '&#xe606;',
        component: () => import('@/views/info/message/Index')
      }
    ]
  },
  {
    path: '/study/:id',
    name: 'study',
    component: () => import('@/views/study/Index')
  },
  
]

// const routerMap = new Map()

// routes.forEach(item => {
//   if (item.unauth) {
//     routerMap.set(item.name)
//   }
// })
const router = new VueRouter({
  routes
})

// 解决Vue-Router升级导致的Uncaught(in promise) navigation guard问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}

const WHITE_ROUTE = [
  'home',
  'login',
  'retrieve',
  'information',
  'informationDetail'
]
router.beforeEach((to, from, next) => {
  if (WHITE_ROUTE.includes(to.name)) {
    next()
  } else {
    if (getToken()) {
      next()
    } else {
      next(`/login?redirect=${to.path}`)
      // next()
    }
  }
})

export default router
